package com.winsdom.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.domain.vo.BuildingResourceVO;
import com.winsdom.dto.CategoryTreeNodes;
import com.winsdom.dto.CategoryTreeNodesParent;
import com.winsdom.entity.Category;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.winsdom.vo.OrganizationMegVO;
import com.winsdom.vo.UserAndOrgPageVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 分类表 Mapper 接口
 * </p>
 *
 * @author 
 * @since 2025-05-12
 */
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {

    /**
     * 根据父节点ID递归查询子节点，构建树形结构
     * @param id
     * @return
     */
    List<CategoryTreeNodes> getCategoryTreeNodes(@Param("id") Long id);
    //包含根节点
    List<CategoryTreeNodes> getCategoryTreeNodesConcludeRoot(@Param("id") Long id);
    @Select("SELECT * FROM category WHERE id = #{id} AND status = 1")
    CategoryTreeNodes getNodeById(@Param("id") Long id);

    // 查询指定节点下所有type<=3的子节点
    @Select("SELECT * FROM category WHERE parent_id = #{parentId} AND type <= 3 AND status = 1")
    List<CategoryTreeNodes> getChildrenByParentId(@Param("parentId") Long parentId);

    // 查询指定节点下所有type<=5的子节点
    @Select("SELECT * FROM category WHERE parent_id = #{parentId} AND type <= 5 AND status = 1")
    List<CategoryTreeNodes> getChildrenByParentId2(@Param("parentId") Long parentId);

    // 查询指定节点下所有type<=6的子节点(查询组织->资源)
    @Select("SELECT * FROM category WHERE parent_id = #{parentId} AND type <= 6 AND status = 1")
    List<CategoryTreeNodes> getChildrenByParentIdBy6(@Param("parentId") Long parentId);

    //查询分类表中所有信息
    @Select("SELECT * FROM category WHERE status = 1")
    List<CategoryTreeNodes> selectAll();


    //修改状态
    @Update("update category set status=#{status} where id=#{id}")
    int updateStatus(@Param("id")Long id,@Param("status")Integer status);

    OrganizationMegVO getOrganizationMsg(Long categoryId);
    String getSuperiorOrganizationName(Long categoryId);

    List<Long> getAllLeaf(Long categoryId);
    //获取所有组织下的所有项目
    List<Long> getAllLeaf2(Long categoryId);
    // 分页查询用户数据
    Page<UserAndOrgPageVo> getUserPage(Page<UserAndOrgPageVo> page,List<Long> leafIds);
    /**
     * 查询同级分类下是否存在同名资源
     */
    List<Category> selectByNameAndParentId(
            @Param("parentId") Long parentId,
            @Param("name") String name,
            @Param("type") Integer type
    );
    @Select("select * from category where id = #{id}")
    Category getById(@Param("id") Integer id);

    @Select("SELECT id from category WHERE type=3")
    List<Long> getAllBuildings();

    List<BuildingResourceVO> getBuildingResource(Long buildingId);

    String getParent(Long categoryId);

    CategoryTreeNodesParent getRootTree(Long category);

}
